package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;

import edu.bit.water.controller.MetaEvaWuseInfoController;
import edu.bit.water.dao.MetaEvaWuseInfoDao;
import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaEvaWsrcInfo;
import edu.bit.water.entity.MetaEvaWuseInfo;

@Service
@Component
public class MetaEvaWuseInfoService {

    //记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
    protected static final Logger logger = LoggerFactory.getLogger(MetaEvaWuseInfoController.class);

    @Autowired
    public MetaEvaWuseInfoDao metaEvaWuseInfoDao;
    @Autowired
    public MetaEvaWuseInfoAssociationService metaEvaWuseInfoAssociationService;
    @Autowired
    public MetaEvaWuseInfoService metaEvaWuseInfoService;
    private boolean flag = false;// 用来记录实体表字段是否发生改变，true为已改变，false为改变
    private List<Column> list = null;
    private List<MetaEvaWuseInfo> listAll = null;

    /**
     * @return 返回所有字段
     */
    public List<Column> selectColumn() {
        if (list == null || flag) {
            list = metaEvaWuseInfoDao.selectColumn();
            flag = false;
        }
        return list;
    }

    /**
     * @return 返回所有字段
     */
    public List<MetaEvaWuseInfo> selectColumnAll() {
        if (listAll == null || flag) {
            listAll = metaEvaWuseInfoDao.queryMetaEvaWuseInfo();
            flag = false;
        }
        return listAll;
    }

    /**
     * @return 返回list
     * @throws JsonProcessingException
     */
    public ArrayList<MetaEvaWuseInfo> queryMetaEvaWuseInfo() throws JsonProcessingException {
        ArrayList<MetaEvaWuseInfo> list = metaEvaWuseInfoDao.queryMetaEvaWuseInfo();
        return list;
    }

    /**
     * 查詢同組列數
     * @param faLevel 組號
     * @return 同組列數
     */
    public int queryColumnNumByFaLevel(int faLevel) {
        return metaEvaWuseInfoDao.queryColumnNumByFaLevel(faLevel);
    }

    /**
     *
     * @return 成功返回1，失败返回0
     */
    public Map<String, Object> insertMetaEvaWuseInfo(MetaEvaWuseInfo metaEvaWuseInfo) {
        //metaEvaWuseInfo.dateChange();
        Map<String, Object> result = new HashMap<>();
        Integer num = metaEvaWuseInfoDao.getMaxId();
        if (num != null) {
            metaEvaWuseInfo.setId(num + 1);
        }
        else {
            metaEvaWuseInfo.setId(0);
        }

        try {
            metaEvaWuseInfoDao.insert(metaEvaWuseInfo);
            metaEvaWuseInfoAssociationService.insertMetaEvaWuseInfoAssociation(metaEvaWuseInfo);

            result.put("status", "success");
            flag = true;
        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("CS_NAME")) {
                result.put("status", "failed");
                result.put("msg", "中文名称已存在，请重新输入");
            }else if(e.getMessage().contains("EN_NAME")) {
                result.put("status", "failed");
                result.put("msg", "英文名称已存在，请重新输入");
            }
        }


        return result;
    }

    /**
     *
     * @return 成功返回1，失败返回0
     */
    public Map<String, Object> deleteMetaEvaWuseInfo(List<String> metaEvaWuseInfo) {
        Map<String, Object> tmpMap = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < metaEvaWuseInfo.size(); i++) {
            int result = metaEvaWuseInfoDao.delete(Integer.parseInt(metaEvaWuseInfo.get(i)));
            if(result == 0) {
                tmpMap.put(metaEvaWuseInfo.get(i), "failed");
                flag = false;
            }
            else {
                tmpMap.put(metaEvaWuseInfo.get(i), "success");
            }
        }
        if (flag) {
            tmpMap.put("status", "success");
        }
        else {
            tmpMap.put("status", "failed");
        }
        return tmpMap;
    }
    /**
     *
     * @return 成功返回1，失败返回0
     */
   // public int deleteMetaEvaWuseInfo(String enName) {
   //     int n = metaEvaWuseInfoDao.deleteMetaEvaWuseInfo(enName);
   //     flag = true;
   //     return n;
   // }

    public Map<String, Object> updateMetaEvaWuseInfo(MetaEvaWuseInfo metaEvaWuseInfo) {


        Map<String, Object> modelMap = new HashMap<>();


        try{
            metaEvaWuseInfoDao.updateMetaEvaWuseInfo(metaEvaWuseInfo);
            if(!metaEvaWuseInfo.getOldName().equals(metaEvaWuseInfo.getEnName())){
                metaEvaWuseInfoAssociationService.updateMetaEvaWuseInfoAssociation(metaEvaWuseInfo);
            }

            modelMap.put("status", "success");
            flag = true;



        }catch (DuplicateKeyException e){
            if(e.getMessage().contains("CS_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "中文名称已存在，请重新输入");
            }else if(e.getMessage().contains("EN_NAME")) {
                modelMap.put("status", "failed");
                modelMap.put("msg", "英文名称已存在，请重新输入");
            }
        }



        return modelMap;
    }
}

